<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BbsListManager
 *
 * @author kambokdol
 */
include_once("{$_SERVER['DOCUMENT_ROOT']}/header.php");
include_once(PHP_ROOT . "/class/db/bbs/BbsList.php");

//게시판기본형
class BbsListManager extends BbsList {

    function init($init_arr) {
        if (empty($init_arr["layer_id"])) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> layer_id 가 없습니다.  ");
        }
        if (empty($init_arr["category"])) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> category 가 없습니다.  ");
        }
        if (empty($init_arr["bbs_path"])) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> bbs_path 가 없습니다.  ");
        }
        if (empty($init_arr["count"])) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> count 가 없습니다.  ");
        }
        if (empty($init_arr["table"])) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> table 가 없습니다.  ");
        }
        if (empty($init_arr["order_by"])) {
            throw new Exception("<br>method : " . __METHOD__ . "<br> order_by 가 없습니다.  ");
        }

        $this->layer_id = $init_arr["layer_id"];
        $this->category = $init_arr["category"];
        $this->target_url = $init_arr["bbs_path"];
        $this->end_no = $init_arr["count"];
        $this->table = $init_arr["table"];
        $this->key_search = $init_arr["key_search"];
        $this->key_search_field = $init_arr["key_search_field"];
        $this->page = $init_arr["page"];
        $this->admin = false;
        $this->order_by = $init_arr["order_by"];
        $this->parent_uid = $init_arr["parent_uid"];
        if (!empty($this->key_search)) {
            $this->where = " and binary(" . $this->key_search_field . ") LIKE '%" . $this->key_search . "%' ";
        }
        $this->total_rows = $this->get_rows();
        $this->start_no = $this->page * $this->end_no;
        $this->bbs_rows = $this->total_rows - $this->start_no;
        $this->total_pages = ceil($this->total_rows / $this->end_no);
    }

    //총줄수 필요하면 오버라이딩
    function get_rows() {
        return $this->get_count($this->table, "where board_table = '$this->category' $this->where");
    }

    function set_search() {
        $this->search = "검색창은 표현하는페이지에서 오버라이딩 해서쓸것";
        echo $this->search;
    }

    function show_list() {
        $sql = "select * from $this->table  where board_table = '$this->category' $this->where order by $this->order_by desc limit $this->start_no,$this->end_no";
        $result = $this->get_result($sql);
        $this->result = $result;
        $this->set_style();
    }

    function show_list_jump() {

        $k = 5;
        $until = $this->page + $k;
        if ($this->page <= 5) {
            $until = 10;
        }
        if ($until >= ($this->total_pages - 1)) {
            $until = $this->total_pages;
        }
        for ($i = ($this->page - $k); $i < $until; $i++) {
            $k++;
            if ($i <= 0) {
                $i = 0;
            }
            if ($this->page == $i) {
                $colorAndBold = "style=\"color:red; font-weight: bold;\"";
            } else {
                $colorAndBold = "";
            }
            $a = $i + 1;

            $list .= " <b><a onClick=\"bbs.swapPage('$this->target_url?bbs_mode=&table=$this->table&page=$i&key_search=$this->key_search&key_search_field=$this->key_search_field&layer_id=$this->layer_id&category=$this->category&num=$this->end_no&order_by=$this->order_by','$this->layer_id');\" class='pageNum' $colorAndBold>$a</a></b> ";

            $back = $this->page + 1;
            $back10 = $this->page + 10;
            $front = $this->page - 1;
            $front10 = $this->page - 10;



            if ($back >= $this->total_pages) {
                $back = $this->total_pages - 1;
            }
            if ($back10 >= $this->total_pages) {
                $back10 = $this->total_pages - 1;
            }
            if ($front >= $this->total_pages) {
                $front = $this->total_pages - 1;
            }
            if ($front10 >= $this->total_pages) {
                $front10 = $this->total_pages - 1;
            }

            if ($back <= 0) {
                $back = 0;
            }
            if ($back10 <= 0) {
                $back10 = 0;
            }
            if ($front <= 0) {
                $front = 0;
            }
            if ($front10 <= 0) {
                $front10 = 0;
            }

            $go1 = "<img src='" . SCRIPT_ROOT . "/img/icon/play.png' border='0' width='16' height='16' align='absmiddle'>";
            $back1 = "<img src='" . SCRIPT_ROOT . "/img/icon/playBack.png' border='0' width='16' height='16' align='absmiddle'>";
            $go2 = "<img src='" . SCRIPT_ROOT . "/img/icon/fast forward.png' border='0' width='16' height='16' align='absmiddle'>";
            $back2 = "<img src='" . SCRIPT_ROOT . "/img/icon/skip forward.png' border='0' width='16' height='16' align='absmiddle'>";

            $b = "<b><a onClick=\"bbs.swapPage('$this->target_url?bbs_mode=&table=$this->table&page=$back&key_search=$this->key_search&key_search_field=$this->key_search_field&layer_id=$this->layer_id&category=$this->category&num=$this->end_no&order_by=$this->order_by','$this->layer_id');\" class='pageNum' $colorAndBold> $go1 </a></b>";
            $f = "<b><a onClick=\"bbs.swapPage('$this->target_url?bbs_mode=&table=$this->table&page=$front&key_search=$this->key_search&key_search_field=$this->key_search_field&layer_id=$this->layer_id&category=$this->category&num=$this->end_no&order_by=$this->order_by','$this->layer_id');\" class='pageNum' $colorAndBold> $back1 </a></b>";
            $bb = "<b><a onClick=\"bbs.swapPage('$this->target_url?bbs_mode=&table=$this->table&page=$back10&key_search=$this->key_search&key_search_field=$this->key_search_field&layer_id=$this->layer_id&category=$this->category&num=$this->end_no&order_by=$this->order_by','$this->layer_id');\" class='pageNum' $colorAndBold> $go2 </a></b>";
            $ff = "<b><a onClick=\"bbs.swapPage('$this->target_url?bbs_mode=&table=$this->table&page=$front10&key_search=$this->key_search&key_search_field=$this->key_search_field&layer_id=$this->layer_id&category=$this->category&num=$this->end_no&order_by=$this->order_by','$this->layer_id');\" class='pageNum' $colorAndBold> $back2 </a></b>";
            $thisPage = $this->page + 1;
        }
        echo "$ff $f $list $b $bb ";
    }

    function show_search() {
        $this->set_search();
    }

}
